<?php

class Account extends MY_Controller {

	function __construct()
	{
		parent::__construct();
		$this->page=$this->module_codes['Account'];
		$this->load->model('account_model' , 'AM');
                $this->load->model('list_price_model' , 'LPM');
                $this->chekup_names = $this->LPM->get_list_price();
	}
	
	function index()
	{
            $this->check_page_permission('access');
            $this->load->view('head');
            $this->load->view('account/main');
            $this->load->view('footer');
            
	}
	
function chceckups_total($patient_id=NULL,$month=NULL,$year=NULL){
	$this->check_page_permission('access');
	if($patient_id=='NULL') $patient_id=NULL;
	return $this->AM->get_chceckups_total($patient_id,$month,$year);
	
	
	
}	

function print_account($patient_id,$preview_date=NULL){
	$condition=array();
	$this->check_page_permission('access');
	if($preview_date!==NULL) $condition=array('preview_date'=>$preview_date);
	$result=$this->AM->get_previews_cost($patient_id,$condition);
           #print_r($result);
             $count = !isset($result['count']) ? NULL : $result['count'];
            
              //if count == 1 thats mean there is no checkup
              $html="<html><head><style>
			body {font-family: Arial}
			
			
			
		</style>
		
		
		<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
		
		</head>
		
		<body>";
             
	     $this->load->model('setting_model', 'SM');
		  $reportSetting=  $this->SM->getSetting('PDF');
	
               	 
     
  
        foreach($result[$patient_id] as $key=>$value)
        {
            $total = 0;
         // $html=
          $html.= '<h3>'.$value['preview_date'].'</h3>';
          
          $html.= "<div><table cellpadding=5 cellspacing=2><tr><th>". lang('form_label_description') ."</th> <th>". lang('menuitem_price')." </th></tr>";
            foreach($value['checkup'] as $checkupID=>$price){
                $html.= "<tr>";
                $html.= "<td>".$this->_get_checkup_name($checkupID)."  </td><td style='border:1px solid gray'> ".$price."</td>";
                $total = $total + $price;
                $html.= "</tr>";
            }

            $html.= "<tr><td style='border:1px solid gray' colspan=2>".lang('form_label_total')." : ".$total." ". lang('form_label_jod')."</td></tr>";
               $html.= "</table></div>";
          }
        $html.= '</div></div>';
   
           $html.= "</body></html>"; 
            ///echo $html;die;
     $this->load->plugin('to_pdf');
     create_pdf($html, 'account_report',$reportSetting);      
            
            
    }

            


function chceckups_cost($patient_id, $is_view=FALSE)
	{
	        $this->check_page_permission('access');
            $result=$this->AM->get_previews_cost($patient_id);
           #print_r($result);
            $count = !isset($result['count']) ? NULL : $result['count'];
            
            if(!$is_view)
                return $this->_show_patient_account($patient_id, $count, $result);
            else{
            $this->load->view('head');
                $this->_show_patient_account($patient_id, $count, $result);
            $this->load->view('footer');
            }
	}

function _get_checkup_name($checkup_id=NULL)
{
   foreach ($this->chekup_names['info'] as $key)
     $chekup_name[$key['id']] = $key['test_name'];

   return $chekup_name[$checkup_id];
}
        /*
         * $p_id : the patient id
         * $count : the counter of the history records
         * $result: the array of history
         */
function _show_patient_account($p_id=NULL, $count=NULL, $result=NULL)
    {
    	$this->check_page_permission('access');
            //if count == 1 thats mean there is no checkup
             $atts = array(
              'scrollbars' => 'yes',
              'status'     => 'yes',
              'resizable'  => 'yes',
              'screenx'    => '0',
              'screeny'    => '0',
              'class'=>'btn'
            );
    	
            if($count == 0)
                    echo lang('general_no_data_found');
            else{
                    echo $count .' '. lang('general_record_found').'<br><br>';
echo '<script type="text/javascript">$(function() {var icons = {header: "ui-icon-circle-arrow-e",headerSelected: "ui-icon-circle-arrow-s"};$("#accordion").accordion({icons: icons});$("#toggle").button().toggle(function() {$("#accordion").accordion("option", "icons", false);}, function() {$("#accordion").accordion("option", "icons", icons);})});</script>';
                    echo '<!-- Start --><div class=""><div id="accordion">';
if(!empty($result[$p_id] ))
        foreach($result[$p_id] as $key=>$value)
        {
            $total = 0;
          echo '<h3><a href="#">'.$value['preview_date'].'</a></h3>';
          
          echo "<div><table cellpadding=5 cellspacing=2><tr><th>". lang('form_label_description') ."</th> <th>". lang('menuitem_price')." </th></tr>";
          
          echo  anchor_popup('account/print_account/'.$p_id.'/'.$value['preview_date'], 'print', $atts);
            foreach($value['checkup'] as $checkupID=>$price){
                echo "<tr>";
                echo "<td>".$this->_get_checkup_name($checkupID)."  </td><td style='border:1px solid gray'> ".$price."</td>";
                $total = $total + $price;
            }

            echo "<tr><td style='border:1px solid gray' colspan=2>".lang('form_label_total')." : ".$total." ". lang('form_label_jod')."</td></tr>";
               echo "</table></div>";
          }
 echo '</div></div><!-- End -->';
            }
    }


	
}

/* End of file account.php */
/* Location: ./system/application/controllers/account.php */
